import md5Libs from "uview-ui/libs/function/md5";
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)


let lifeData

const localStorageCurrentUserKey = 'xiaolvknowlaw_wxmp_current_user';

let localStorageValue = uni.getStorageSync(localStorageCurrentUserKey);

try {
	lifeData = uni.getStorageSync(localStorageValue);
} catch (e) {
	console.log(e)
}

// 需要永久存储，且下次APP启动需要取出的，在state中的变量名
const saveStateKeys = ['userInfo', 'localFile']

// 保存变量到本地存储中
const saveLifeData = function(key, value) {
	// localStorageValue = uni.getStorageSync(localStorageCurrentUserKey);
	// 判断变量名是否在需要存储的数组中
	if (saveStateKeys.indexOf(key) != -1) {
		// 获取本地存储的lifeData对象，将变量添加到对象中
		let tmp = uni.getStorageSync(localStorageValue);
		// 第一次打开APP，不存在lifeData变量，故放一个{}空对象
		tmp = tmp ? tmp : {};
		tmp[key] = value;
		// 执行这一步后，所有需要存储的变量，都挂载在本地的lifeData对象中
		uni.setStorageSync(localStorageValue, tmp);
	}
}




const store = new Vuex.Store({
	state: {
		userInfo: lifeData.userInfo ? lifeData.userInfo : null,
		shareCode:'',
		// localFile: lifeData.localFile ? lifeData.localFile : null,
		currentSessionId: undefined,
		globalUnreadNum: 0,
		networkStatus: undefined,
		networkConnected:true,
		// companyNumber:'',
		// reviewStatus:false,
		// vipModalContent:'',
		wxLoginResult: null,
		currentLawyerId:'',
		isGetSessionCompleted:false,
	},
	mutations: {
		$stateSave(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.');
			let saveKey = '';
			let len = nameArr.length;
			if (len >= 2) {
				let obj = state[nameArr[0]];
				for (let i = 1; i < len - 1; i++) {
					obj = obj[nameArr[i]];
				}
				obj[nameArr[len - 1]] = payload.value;
				saveKey = nameArr[0];
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value;
				saveKey = payload.name;
			}
		},
		$localSave(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.');
			let saveKey = '';
			let len = nameArr.length;
			if (len >= 2) {
				let obj = state[nameArr[0]];
				for (let i = 1; i < len - 1; i++) {
					obj = obj[nameArr[i]];
				}
				obj[nameArr[len - 1]] = payload.value;
				saveKey = nameArr[0];
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value;
				saveKey = payload.name;
			}
			// 保存变量到本地，见顶部函数定义;
			saveLifeData(saveKey, state[saveKey])
		},
		$setCurrentUserLabel(state, payload) {
			return new Promise((resolve, reject) => {
				localStorageValue = md5Libs.md5(`${localStorageCurrentUserKey}${payload}`);
				lifeData = uni.getStorageSync(localStorageValue);
				uni.setStorage({
					key: localStorageCurrentUserKey,
					data: md5Libs.md5(`${localStorageCurrentUserKey}${payload}`),
					success(res) {
						resolve()
					},
					fail(e) {
						reject()
					}
				})
			})

		},
		$removeCurrentUserLabel(state, payload) {
			uni.removeStorageSync(localStorageCurrentUserKey);
			state.isGetSessionCompleted = false;
			// state.userInfo = null;
		},
		$getUserLocalStorage(state, payload) {
			// state.userInfo = lifeData.userInfo ? lifeData.userInfo : null;
			// state.localFile = lifeData.localFile ? lifeData.localFile : null;
		},
		$setCurrentSessionId(state, payload) {
			state.currentSessionId = payload;
		},
	},
	actions: {

	},

})



export default store
